package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.PregnantInfo;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.Utils;

/**
 * Created by ray_lei on 2016/7/14.
 */
public class PregnantInfoActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_edit;

    private EditText wekksET;
    private EditText expectDateET;
    private EditText birthDateET;

    private ObjectResultBean<PregnantInfo> resultBean;

    private String menstrualStartDate = "";
    private String menstrualCycle = "";

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_MYDELIVERYINFO_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<PregnantInfo>) msg.obj;
                    if(resultBean.getData() != null){
                        wekksET.setText(resultBean.getData().getGestationalWeeks());
                        expectDateET.setText(Utils.timeStamp2Date(resultBean.getData().getPredictedDate(), "yyyy-MM-dd"));
//                        birthDateET.setText(resultBean.getData().getDeliveryDate());
                        birthDateET.setText(Utils.timeStamp2Date(resultBean.getData().getDeliveryDate(), "yyyy-MM-dd HH:mm"));

                        menstrualStartDate = resultBean.getData().getMenstrualStartDate();
                        menstrualCycle = resultBean.getData().getMenstrualCycle();
                    }

                }

                break;
            case MessageCode.NETWORKCODE_MYDELIVERYINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_pregnantinfo);
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_edit = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.pregnantinfo_text_title));
        tv_edit.setVisibility(View.VISIBLE);
        tv_edit.setText(getResources().getText(R.string.pregnantinfo_text_edit));
        iv_back.setOnClickListener(this);
        tv_edit.setOnClickListener(this);

        wekksET = (EditText) findViewById(R.id.pregnantinfo_et_weeks);
        expectDateET = (EditText) findViewById(R.id.pregnantinfo_et_expectdate);
        birthDateET = (EditText) findViewById(R.id.pregnantinfo_et_birthdate);

    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        tv_edit.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        YiJiaHuNetWorkUtils.getDeliveryCycleInfo(handler);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back://back
                finish();
                break;
            case R.id.tv_title_func://edit
                Intent editIntent = new Intent(PregnantInfoActivity.this, PregnantEditInfoActivity.class);
                editIntent.putExtra("menstrualStartDate",Utils.timeStamp2Date(menstrualStartDate, "yyyy-MM-dd"));
                editIntent.putExtra("menstrualCycle",menstrualCycle);
                editIntent.putExtra("birthDate",birthDateET.getText().toString().trim());
                startActivity(editIntent);
                finish();
                break;
        }
    }
}
